@extends('admin.common.admin_main')
@section('container')
<div class="uc-body">
    <div class="uc-head">
        <ul>
            <li><a href="/user/device_edit?device_id={{$device_id}}">编辑设备</a></li>
            <li><span class="sel">管理型号</span></li>
        </ul>
    </div>
    <div class="form form-inline">
        <div class="form-body">
            <table class="table">
                <thead>
                <tr>
                    @for ($i = 0; $i < count($th_list); $i++)
                    <th>{{$th_list[$i]}}</th>
                    @endfor
                    <th data-th="{{implode(',',$th_list)}}">操作</th>
                </tr>
                </thead>
                <tbody>
                @forelse ($td_list as $key=>$value)
                <tr>
                    <?php $paramDataList = array_slice($td_list[$key], 1);?>
                    @for ($i = 0; $i < count($paramDataList); $i++)
                    <td>
                        <div class="tit">{{$paramDataList[$i]}}</div>
                    </td>
                    @endfor
                    <td>
                        <input class="j_id" type="hidden" value="{{$value[0]}}">
                        <a class="j_edit" data-param="{{implode(',',$paramDataList)}}">编辑</a>
                        <span class="split">|</span>
                        <a class="j_del">删除</a>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="{{count($th_list)+1}}">暂无型号</td>
                </tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="pop-bg j_popBg" style="display: none"></div>
<div class="pop pop-body j_popFrame" style="display: none">
    <a class="close close-white j_frameClose">关闭</a>
    <div id="iModel" class="pop-device pop-device-model">
        <input type="hidden" id="numberId" value="">
        <div class="title">编辑型号</div>
        <div class="form-body"></div>
    </div>
</div>
@endsection

@section('script')
<script>
    (function ($) {
        $(function () {
            $(".j_edit").click(function () {
                var $t = $(this);
                $("#numberId").val($t.parent().find('.j_id').val());
                var tit = $('[data-th]').attr('data-th').split(",");
                var param = $t.attr('data-param').split(",");
                var html = '';
                var $model = $("#iModel");
                for (var i = 0; i < param.length; i++) {
                    html += '<div class="row">' +
                            '<label class="label"><span class="require">*</span>' + tit[i] + '：</label>' +
                            '<div class="control-text"><input class="form-control" type="text" value="' + param[i] + '"></div>' +
                            '</div>';
                }
                html += '' +
                        '<div class="row text-center">' +
                        '<a class="btn btn-small btn-warning j_saveModel">保存</a>' +
                        '<a class="btn btn-small btn-primary j_closePop">取消</a>' +
                        '</div>';
                $model.find(".form-body").html(html);
                $(".j_popBg,.j_popFrame").fadeIn(200);
                $('.j_popFrame').css({
                    "width": $model.width() + "px",
                    "margin-top": -$model.height() / 2 + "px",
                    "margin-left": -$model.width() / 2 + "px"
                });
            });

            $("#iModel").on("click", ".j_saveModel", function () {//保存型号
                var $ipt = $("#iModel").find(".form-control");
                var param_list_info = "";
                for (var i = 0; i < $ipt.length; i++) {
                    var $t = $ipt.eq(i);
                    var val = $t.val();
                    if (!val) {
                        return showErr($t, "此项必须填写");
                    }
                    param_list_info += "," + val;
                }
                param_list_info = param_list_info.substr(1);
                var id = $("#numberId").val();
                var dataObj = {
                    id: id,
                    param_list_info: param_list_info,
                    action: 'edit_device_number_info',
                    device_id:{{$device_id}}
                };
                var $btn = $(this);
                $.ajax({
                    url: "/user/action",
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    data: dataObj,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data["result"] == 0) {
                            $.Alert({
                                obj: $btn, txt: '保存成功！', callback: function () {
                                    location.href = "/user/device_number?device_id="+{{$device_id}};
                                }
                            });
                        } else {
                            $.Alert({
                                obj: $btn, txt: data["msg"], type: 'danger'
                            });
                        }
                    }
                });
            }).on("click", ".j_closePop", function () {//取消
                $(".j_popBg,.j_popFrame").fadeOut(200);
                setTimeout(function () {
                    $(".j_popFrame").attr({"style": ""}).css("display", "none");
                    $("#iFrame").attr({"src": "", "style": ""});
                }, 250);
            });
            //删除
            $(".j_del").click(function () {
                var $t = $(this);
                var id = $t.parent().find(".j_id").val();
                $.Alert({
                    dir: 'right',
                    obj: $t,
                    txt: '确认要删除吗？ <a class="sure j_sureDel">确定</a> <a class="cancel j_cancelDel">取消</a>',
                    type: 'danger',
                    callback: function () {
                    }
                });
                $(".j_sureDel").click(function () {
                    if (id) {
                        $.ajax({
                            url: "/user/action",
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            data: {
                                id: id,
                                action: 'del_device_number_info',
                                device_id:{{$device_id}}
                            },
                            type: "POST",
                            dataType: "json",
                            success: function (data) {
                                if (data["result"] == 0) {
                                    var result_id = data["id"];
                                    $.Alert({
                                        obj: $t, txt: '删除成功！', callback: function () {
                                            $t.parents("tr").remove();
                                        }
                                    });
                                } else {
                                    $.Alert({
                                        obj: $t, txt: data["msg"], type: 'danger'
                                    });
                                }
                            }
                        });
                    }
                });
                $(".j_cancelDel").click(function () {
                    $("#j_alert").remove();
                });
            });

        });
    })(jQuery);
</script>
@endsection